package com.baicaoyuan.web.controller;

import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.enums.CrowdfundingStatusEnum;
import com.baicaoyuan.web.request.br.*;
import com.baicaoyuan.web.response.CrowdfundingResponse;
import com.baicaoyuan.web.response.CrowdfundingUserResponse;
import com.baicaoyuan.web.service.CrowdfundingService;
import com.baicaoyuan.web.service.CrowdfundingUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2019/6/4
 * 版权：百草园电子商务
 */
@Api(value = "众筹Controller", tags = {"众筹操作接口"})
@RestController
@RequestMapping(value = "/crowdfunding")
public class CrowdfundingController {

    @Autowired
    CrowdfundingService crowdfundingService;

    @Autowired
    CrowdfundingUserService crowdfundingUserService;

    @ApiOperation(value = "获取一个众筹详情（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/findOne", method = RequestMethod.POST)
    @ResponseBody
    public Status findOne(@RequestBody BCrowdfundingIdRequest request) {
        try {
            CrowdfundingResponse response = crowdfundingService.findOne(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", response);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "前端-获取众筹列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getCrowdfundings", method = RequestMethod.POST)
    @ResponseBody
    public Status getCrowdfundings(@RequestBody BCrowdfundingPageRequest request) {
        try {
            List<String> status = Arrays.asList(new String[]{CrowdfundingStatusEnum.CLOSE.getStatus(),
                    CrowdfundingStatusEnum.DELETE.getStatus(),
                    CrowdfundingStatusEnum.INIT.getStatus()});
            List<CrowdfundingResponse> crowdfundings = crowdfundingService.getCrowdfundings(status, request.getPage(), request.getSize());
            Integer count = crowdfundingService.getCountOfAll(status);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("crowdfundings", crowdfundings);
            result.setData("totalCount", count);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-获取众筹列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getCrowdfundingsBg", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getCrowdfundingsBg(@RequestBody BCrowdfundingPageRequest request) {
        try {
            List<String> status = Arrays.asList(new String[]{CrowdfundingStatusEnum.DELETE.getStatus()});
            List<CrowdfundingResponse> crowdfundings = crowdfundingService.getCrowdfundings(status, request.getPage(), request.getSize());
            Integer count = crowdfundingService.getCountOfAll(status);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("crowdfundings", crowdfundings);
            result.setData("totalCount", count);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-新建众筹（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status save(@RequestBody BCrowdfundingRequest request) {
        try {
            crowdfundingService.save(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "新加众筹参与用户（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveUser(@RequestBody BCrowdfundingUserRequest request) {
        try {
            crowdfundingUserService.save(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "获取参与用户列表（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST)
    @ResponseBody
    public Status getUsers(@RequestBody BCrowdfundingIdRequest request) {
        try {
            List<CrowdfundingUserResponse> r = crowdfundingUserService.getUsers(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("users", r);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-删除一个众筹详情（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status delete(@RequestBody BCrowdfundingIdRequest request) {
        try {
            boolean b = crowdfundingService.delete(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", b);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-关闭一个众筹详情（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status close(@RequestBody BCrowdfundingIdRequest request) {
        try {
            boolean b = crowdfundingService.close(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", b);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-更新状态一个众筹详情（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status updateStatus(@RequestBody BCrowdfundingIdStatusRequest request) {
        try {
            boolean b = crowdfundingService.updateStatus(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", b);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-编辑众筹（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status edit(@RequestBody BCrowdfundingEditRequest request) {
        try {
            crowdfundingService.edit(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

}
